package com.fourfinance.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.UUID;

import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.ResourceBundleMessageSource;

import com.fourfinance.exception.LenderException;

public class LenderUtil {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(LenderUtil.class);
	
	private static DateFormat df = new SimpleDateFormat(LenderConstants.DATE_FORMAT);
	private static ResourceBundleMessageSource msgSource;
	
	static {
		msgSource = new ResourceBundleMessageSource();
        msgSource.setBasename(LenderConstants.RESOURCES);
        msgSource.setUseCodeAsDefaultMessage(true);
	}
	
    public static String getMessage(String messageCode, Object... messageParameters) {
        Locale current = LocaleContextHolder.getLocale();
        LOGGER.debug("Current locale is {}", current);
        return msgSource.getMessage(messageCode, messageParameters, current);
    }	
	
	/**
	 * Generate unique Reference ID String
	 * @return uuidObject String
	 */
	public static String generateReference(){
        UUID uuidObject = UUID.randomUUID();
        return uuidObject.toString();
	}
	
	/**
	 * Convert String value to date using generic date format
	 * 
	 * @param dateString
	 * @return
	 * @throws LenderException
	 */
	public static Date convertStringToDate(String dateString) throws LenderException{
		try {
			return df.parse(dateString);
		} catch (ParseException e) {
			throw new LenderException(e.getLocalizedMessage());
		}
	}
	
	public static String convertDateToString(Date date) throws LenderException{
		return df.format(date);
	}
	
	/**
	 * Get minimum application time for Loan
	 * 
	 * @return
	 */
	public static Date getMinTime(){
		Date minDate = DateUtils.setHours(new Date(), LenderConstants.MIN_HOUR);
		minDate = DateUtils.setMinutes(minDate, 0);
		minDate = DateUtils.setSeconds(minDate, 0);
		return minDate;
	}
	
	/**
	 * Get maximum application time for Loan
	 * 
	 * @return
	 */
	public static Date getMaxTime(){
		Date maxDate = DateUtils.setHours(new Date(), LenderConstants.MAX_HOUR);
		maxDate = DateUtils.setMinutes(maxDate, 0);
		maxDate = DateUtils.setSeconds(maxDate, 0);
		return maxDate;
	}
	
	/**
	 * Check if the attempted date is today
	 * 
	 * @param attemptDate
	 * @return
	 */
	public static Boolean isToday(Date attemptDate){
		
		Date currentDate = new Date();
		currentDate = DateUtils.setHours(currentDate, 0);
		currentDate = DateUtils.setMinutes(currentDate, 0);
		currentDate = DateUtils.setSeconds(currentDate, 0);
		
		attemptDate = DateUtils.setHours(attemptDate, 0);
		attemptDate = DateUtils.setMinutes(attemptDate, 0);
		attemptDate = DateUtils.setSeconds(attemptDate, 0);
		
		return DateUtils.isSameDay(currentDate,attemptDate);
	}
	
	/**
	 * Get extension interest
	 * 
	 * @param interestBefore
	 * @return
	 */
	public static Float getExtensionInterest(Float interestBefore){
		Float newInterest = interestBefore * LenderConstants.EXTENSION_FACTOR;
		return newInterest;
	}
	
	/**
	 * Extend term of the Loan by the week number defined under {@link com.fourfinance.util.LenderConstants LenderConstants}
	 * @param term
	 * @return
	 */
	public static Date extendTerm(Date term){
		return DateUtils.addWeeks(term, LenderConstants.EXTENSION_WEEK_NO);
	}

}
